package main

import (
	_ "haproxy-mgr-go/pre-start"
	"os"
	"haproxy-mgr-go/models/global_config"
	"fmt"
	"haproxy-mgr-go/clusters"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/context"
	_ "haproxy-mgr-go/routers"
)

func main() {
	beego.Get("/hello",func(ctx *context.Context){
		list := clusters.GetMemberlist()
		for k, v:= range list {
			fmt.Println("k=>"+k+", v=>"+v)
		}

		ctx.Output.Body([]byte("hello world"))
	})
	orm.RunCommand()
	runInitTableCommand()
	beego.Run("0.0.0.0:9090")

}

func runInitTableCommand()  {
	if len(os.Args) < 2 || os.Args[1] != "initData" {
		return
	}

	globalConfigs := global_config.FindAll()

	if len(globalConfigs) == 0 {
		global_config.InitConfigs()
	}

	os.Exit(0)
}